package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UploadController {

    //封面图片上传
    @RequestMapping("/uploadImg")
    @ResponseBody
    public Object uploadImg(MultipartFile pic) {
        //将pic数据存储到项目文件中
        //通过oss提供的api将图片数据上传到阿里云的oss服务器中
        String url = null;
        try {
            url = UploadUtil.uploadAli(pic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }
    //
    //https://myluowowo.oss-cn-shenzhen.aliyuncs.com/d4730640-df49-4ad0-8b76-66c3ced52538.jpeg
    //http://localhost:9999/strategy/myluowowo.oss-cn-shenzhen.aliyuncs.com/7eba0410-33c9-401f-a21e-80939d3d420b.jpg

    //处理富文本编辑框中上传图片
    @RequestMapping("/uploadImg_ck")
    @ResponseBody
    public Map<String, Object> upload(MultipartFile upload, String module){
        Map<String, Object> map = new HashMap<String, Object>();
        String imagePath= null;
        if(upload != null && upload.getSize() > 0){
            try {
                //图片保存, 返回路径
                imagePath =  UploadUtil.uploadAli(upload);//将文件传给上传工具，保存到OSS中，返回文件的url
                //表示保存成功
                map.put("uploaded", 1);
                map.put("url",imagePath);
            }catch (Exception e){
                e.printStackTrace();
                map.put("uploaded", 0);
                Map<String, Object> mm = new HashMap<String, Object>();
                mm.put("message",e.getMessage() );
                map.put("error", mm);
            }
        }
        return map;
    }

}
